package com.qupai.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qupai.constant.MessageConstant;
import com.qupai.constant.StatusConstant;
import com.qupai.dto.PrizesDTO;
import com.qupai.dto.PrizesPageQueryDTO;
import com.qupai.entity.Prizes;
import com.qupai.exception.DeletionNotAllowedException;
import com.qupai.mapper.PrizesMapper;
import com.qupai.result.PageResult;
import com.qupai.service.PrizesService;
import com.qupai.vo.PrizesVO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class PrizesServiceImpl extends ServiceImpl<PrizesMapper,Prizes> implements PrizesService {

    @Resource
    private PrizesMapper prizesMapper;

    /**
     * 新增奖品
     */
    @Override
    @Transactional//事务注解，要么全成功，要么全失败,在启动类已经开启事务注解
    public void save(PrizesDTO prizesDTO) {
        Prizes prizes=new Prizes();
        BeanUtils.copyProperties(prizesDTO,prizes);

        Long number= 0L;
        //设置默认被收藏数量
        prizes.setFavoritesCount(number);

        //插入一条数据
        prizesMapper.insert(prizes);

    }

    /**
     * 奖品分页查询
     */
    @Override
    public PageResult pageQuery(PrizesPageQueryDTO prizesPageQueryDTO) {
        Page<Prizes>page=prizesPageQueryDTO.toMpPageDefaultSortByUpdateTimeDesc();
        Page<Prizes>p=lambdaQuery()
                .like(prizesPageQueryDTO.getName()!=null,Prizes::getName,prizesPageQueryDTO.getName())
                .eq(prizesPageQueryDTO.getOrganizationsId()!=null,Prizes::getOrganizationsId,prizesPageQueryDTO.getOrganizationsId())
                .eq(prizesPageQueryDTO.getStatus()!=null,Prizes::getStatus,prizesPageQueryDTO.getStatus())
                .page(page);
        return new PageResult(p.getTotal(),p.getPages(),p.getRecords());

    }

    /**
     * 按照ids批量删除奖品
     */
    @Override
    public void deleteBatch(List<Long> ids) {
        for (Long id : ids) {
//            Prizes prizes=prizesMapper.getById(id);
            Prizes prizes=prizesMapper.selectById(id);
            //起售中不能删除
            if (prizes.getStatus().equals(StatusConstant.ENABLE) ){
                throw new DeletionNotAllowedException(MessageConstant.PRIZES_ON_SALE);
            }
        }

        prizesMapper.deleteBatchIds(ids);
    }

    /**
     * 根据id查询奖品
     */
    @Override
    public PrizesVO getById(Long id) {
        Prizes prizes=prizesMapper.selectById(id);
        PrizesVO prizesVO=new PrizesVO();
        //将查询到的奖品信息复制到VO对象中
        BeanUtils.copyProperties(prizes,prizesVO);
        return prizesVO;
    }

    /**
     * 修改奖品
     */
    @Override
    public void update(PrizesDTO prizesDTO) {
        Prizes prizes=new Prizes();
        BeanUtils.copyProperties(prizesDTO,prizes);
        prizesMapper.updateById(prizes);
    }

    /**
     * 根据id启用或禁用奖品的状态
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        Prizes prizes = Prizes.builder()
                .id(id)
                .status(status)
                .build();
        prizesMapper.updateById(prizes);
    }

    /**
     * 根据部门分类id查询奖品
     */
    @Override
    public List<Prizes> list(Long organizationsId) {
        Prizes prizes = Prizes.builder()
                .status(StatusConstant.ENABLE)
                .organizationsId(organizationsId)
                .build();
        return prizesMapper.list(prizes);
    }

    /**
     * 根据学院分类id查询奖品
     */
    @Override
    public List<PrizesVO> selectPrizesList(Prizes prizes) {
        List<Prizes>PrizesList=prizesMapper.list(prizes);
        List<PrizesVO> prizesVOList=new ArrayList<>();
        for (Prizes p:PrizesList){
            PrizesVO prizesVO=new PrizesVO();
            BeanUtils.copyProperties(p,prizesVO);
            prizesVOList.add(prizesVO);
        }
        return prizesVOList;
    }
}
